package com.itheima.service.impl;

import com.itheima.common.entity.Result;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void upload(MultipartFile excelFile) {
        try {
            XSSFWorkbook excel = new XSSFWorkbook(excelFile.getInputStream());

            XSSFSheet sheet = excel.getSheetAt(0);
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                //获取每一行的数据
                XSSFRow row = sheet.getRow(i);
                //获取每一行的日期
                XSSFCell cell1 = row.getCell(0);
                cell1.setCellType(CellType.STRING);
                String orderDate = row.getCell(0).getStringCellValue();
                SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
                if(orderDate == null){
                    continue;
                }
                Date date = ft.parse(orderDate);

                //获取每一行的可预约人数
                XSSFCell cell = row.getCell(1);
                cell.setCellType(CellType.STRING);
                String num = cell.getStringCellValue();
                int number = Integer.parseInt(num);
                if(number == 0){
                    continue;
                }
                //将数据封装到OrderSetting对象中
                OrderSetting orderSetting =new OrderSetting(date, number);

                //判断当前日期是否已经存在
                if(orderSettingMapper.findByOrderDate(date) != null){
                    //如果存在，则修改
                    orderSettingMapper.editReservationsByOrderDate(orderSetting);
                }else{
                    //如果不存在，则添加
                    orderSettingMapper.add(orderSetting);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void editReservationsByOrderDate(OrderSetting orderSetting) {
        //判断当前日期是否已经存在
        if(orderSettingMapper.findByOrderDate(orderSetting.getOrderDate()) == null){
            //如果不存在，则添加
            orderSettingMapper.add(orderSetting);
            return;
        }

        orderSettingMapper.editReservationsByOrderDate(orderSetting);
    }

    @Override
    public List<OrderSetting> getOrderSettingByMonth(String orderDate) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            Date startDate = format.parse(orderDate + "-01");
            Date endDate = format.parse(orderDate + "-31");

            return orderSettingMapper.getOrderSettingByMonth(startDate, endDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
